package cn.rxls.community.service.impl;

import cn.rxls.community.dto.orders.OrdersDetailCategoryDTO;
import cn.rxls.community.dto.orders.OrdersPageCategoryDTO;
import cn.rxls.community.entity.Maintenance;
import cn.rxls.community.entity.Orders;
import cn.rxls.community.enums.OrdersStateEnum;
import cn.rxls.community.enums.OrdersTypeEnum;
import cn.rxls.community.mapper.BillMapper;
import cn.rxls.community.mapper.MaintenanceMapper;
import cn.rxls.community.mapper.OrdersMapper;
import cn.rxls.community.service.OrdersService;
import cn.rxls.community.util.HttpUtil;
import cn.rxls.community.vo.orders.OrdersVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * (Orders)表服务实现类
 */
@Service("ordersService")
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    private final OrdersMapper ordersMapper;

    private final MaintenanceMapper maintenanceMapper;

    private final BillMapper billMapper;


    OrdersServiceImpl(OrdersMapper ordersMapper,BillMapper billMapper, MaintenanceMapper maintenanceMapper) {
        this.ordersMapper = ordersMapper;
        this.maintenanceMapper = maintenanceMapper;
        this.billMapper = billMapper;
    }

    /**
     * 根据分类分页查询
     *
     * @param dto 参数对象
     * @return 多条订单信息
     */
    @Override
    public List<OrdersVO> selectPageByCategory(OrdersPageCategoryDTO dto) {
        //获取用户信息
        String uid = String.valueOf(HttpUtil.getUserLoginInfo().get("uid"));

        //获取当前状态码
        Integer stateCode = OrdersStateEnum.getCodeByMsg(dto.getState());

        //分页处理
        dto.setPage((dto.getPage()-1)*dto.getSize());
        return ordersMapper.selectListByState(dto.getPage(),dto.getSize(),Long.valueOf(uid),stateCode);
    }


    /**
     * 根据类型、id查询订单详情
     *
     * @param dto 参数对象
     * @return 单条数据
     */
    @Override
    public Object selectDetailByCategoryAndId(OrdersDetailCategoryDTO dto) {
        //获取用户信息
        String uid = String.valueOf(HttpUtil.getUserLoginInfo().get("uid"));

        if (dto.getCategory().equals(OrdersTypeEnum.ORDERS_MAINTENANCE.msg)) {
            LambdaQueryWrapper<Maintenance> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Maintenance::getMaintenanceId, dto.getId()).eq(Maintenance::getMaintenanceUser,Long.valueOf(uid));
            return maintenanceMapper.selectOne(wrapper);
        }


        return null;
    }
}

